mod second;
mod utils;
use utils::files::check_files;
use utils::timer::get_time;
mod thrid;
use std::thread;
use std::time::Duration;
use thrid::get_point;
use utils::thread_util::spawn_function;

fn main() {
    // 初识
    println!("Hello, world!");
    // 变量
    let mut a = 123;
    println!("初始化变量a = {}", a);
    a = 456;
    println!("重置变量a = {}", a);
    // 常量
    const B: i32 = 123;
    println!("初始化常量B = {}", B);
    // 重影 - 变量重新绑定
    let x = 2;
    let x = x + 1;
    let x = x * 8;
    println!("重影变量x = {}", x);
    //复合类型
    let tup: (i32, f64, u8) = (500, 6.4, 1);
    println!("重影复合类型tup = {:?}", tup);
    // 数组
    let arr1 = [1, 2, 3, 4, 5];
    println!("数组a = {:?}", arr1);
    println!("数组第三位 = {}", arr1[2]);
    // 长度为5的i32数组
    let arr2: [i32; 5] = [1, 2, 3, 4, 5];
    println!("长度为5的i32数组a = {:?}", arr2);
    // 等同于 arr3 = [3, 3, 3, 3, 3]
    let arr3 = [3; 5];
    println!("等同于 arr3 = {:?}", arr3);
    // 不可变数组
    let arr4 = [1, 2, 3, 4, 5];
    // 数组长度为5，但第四位修改为6
    println!("不可以直接改变{:?}", arr4);
    // 可变数组
    let mut arr5 = [1, 2, 3, 4, 5];
    arr5[3] = 6;
    println!("有mut关键词，可变数组a = {:?}", arr5);
    auth_function(&mut arr5);
    not_apply_mut(arr5);
    let result = get_res_function();
    println!("获取到的返回值res = {}", result);
    let result2 = get_add_function();
    println!("获取到的函数返回值result2 = {}", result2);
    let result3 = is_even();
    println!("获取到的判断函数返回值result3 = {}", result3);
    while_function();
    println!("调用第二个模块");
    println!("{}", second::message());
    println!("调用访问权限");
    println!("{}", nation::goverment::govern());
    println!("super关键词调用：{:?}", nation::court::judical());
    println!("字段权限");
    second::eat_at();
    check_files();
    get_time();
    // 传入x，y坐标，计算点到原点的距离
    let point = get_point(3.0, 4.0);
    println!("点到原点的距离是{}", point);
    println!("多线程");
    thread::spawn(spawn_function);
    for i in 0..3 {
        println!("main thread print {}", i);
        thread::sleep(Duration::from_millis(1));
    }
}

fn auth_function(arr: &mut [i32; 5]) {
    println!("调用修改数组函数");
    arr[0] = 10;
    println!("修改数组a = {:?}", arr);
}

fn not_apply_mut(arr: [i32; 5]) {
    println!("调用不修改数组函数");
    println!("修改数组a = {:?}", arr);
}
/**
  获取返回值，需要在function后面跟这要返回的类型
*/
fn get_res_function() -> &'static str {
    let res = "hello";
    return res;
}
/**
  函数的语句和表达式
  函数表达式不能有return
*/
fn get_add_function() -> i32 {
    fn x() -> i32 {
        5
    }
    return x() + 10;
}

// 判断函数
fn is_even() -> i32 {
    let a = 12;
    let b;
    if a > 0 {
        b = 1;
    } else if a < 0 {
        b = -1;
    } else {
        b = 0;
    }
    return b;
}

// 循环函数
fn while_function() {
    let mut number = 4;
    while number > 0 {
        println!("{}", number);
        number = number - 1;
    }
    println!("while循环结束");
    println!("for in循环");
    let arr = [10, 20, 30, 40, 50];
    for i in arr.iter() {
        println!("arr循环{}", i);
    }
    println!("for in循环结束");
}

// 访问权限 public和private
mod nation {
    pub mod goverment {
        pub fn govern() -> &'static str {
            let s = "环境";
            return s;
        }
    }
    // super关键词,用来同级调用
    pub mod court {
        pub fn judical() -> &'static str {
            return super::goverment::govern();
        }
    }
}
